<?php
/**
 * Created by PhpStorm.
 * User: xiaosq
 * Date: 2018/6/20
 * Time: 18:21
 */

namespace App\Http\Controllers\Api\Meeting;

use App\Http\Controllers\Admin\AdminController;
use App\Models\Meeting;
use App\Models\MeetingUser;
use Illuminate\Http\Request;
use Validator;
use Lang;
use App\Handlers\Oss;

class UsersController extends AdminController
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request, $meeting_id, MeetingUser $meetingUser)
    {
        $per_page = $request->input('per_page', $this->per_page);
        $status = $request->status;

        $current_query = $meetingUser->select('id', 'real_name', 'mobile', 'face_image', 'tag', 'beauty', 'status', 'sign_in_at')->where('meeting_id', $meeting_id);

        if (!empty($status)) {
            $current_query->where('status', $status);
        }

        $data = $current_query->orderBy('sign_in_at', 'ASC')->paginate($per_page);

        if (!empty($data)) {
            foreach ($data as $key => $val) {
                $face_image_url = '';
                if (!empty($val->face_image)) {
                    $face_image_url = Oss::getPublicObjectURL($val->face_image);
                }
                $data[$key]['face_image_url'] = $face_image_url;
            }
        }

        $meeting_info = Meeting::select('sign_up_num', 'sign_in_num')->where('id', $meeting_id)->first();

        $result['list'] = $data;
        $result['meeting_info'] = $meeting_info;

        return $this->getResponseSuccess($result);
    }

    /**
     * 获取用户所有头像列表
     * @param Request $request
     * @param $meeting_id
     * @param MeetingUser $meetingUser
     */
    public function allFaceImage(Request $request, $meeting_id, MeetingUser $meetingUser)
    {
        $data = $meetingUser->select('mobile', 'face_image', 'created_at')->where('status', '<>', 0)->where('meeting_id', $meeting_id)->orderBy('created_at', 'DESC')->get();

        $result = null;
        $data = $data->toArray();
        if (!empty($data)) {
            $created_at = $data[0]['created_at'];
            foreach ($data as $key => $val) {
                $face_image_url = '';
                if (!empty($val['face_image'])) {
                    $face_image_url = Oss::getPublicObjectURL($val['face_image']);
                }
                $data[$key]['face_image_url'] = $face_image_url;

                unset($data[$key]['face_image']);
                unset($data[$key]['created_at']);
            }

            $result['list'] = $data;
            $result['last_created_at'] = $created_at;
        }

        return $this->getResponseSuccess($result);
    }
}